package org.example.java.util;

import java.util.Timer;
import java.util.concurrent.TimeUnit;

/**
 * 定时策略
 * Created by Juntao.Zhang on 2014/6/3.
 */
public class TimerManager {

  //时间间隔
  private static final long PERIOD_TIME = 5 * 1000;
  private static final long DELAY = 10 * 1000;

  private static synchronized Timer getInstance() {
    if (timer == null)
      timer = new Timer("timer manager", true);
    return timer;
  }

  static Timer timer;

  public static void main(String[] args) throws InterruptedException {

    timer = getInstance();

//    timer.schedule(
//        new java.util.TimerTask() {
//          public void run() {
//            System.out.println("a:" + System.currentTimeMillis());
//          }
//        }, DELAY, PERIOD_TIME
//    );

    timer.schedule(
        new java.util.TimerTask() {
          public void run() {
            System.out.println("b:" + System.currentTimeMillis());
            try {
              TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
              e.printStackTrace();
            }
          }
        }, 0, 1000
    );

    TimeUnit.SECONDS.sleep(100);
  }

}